//
//  MeAuthenticationViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/10.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "MeAuthenticationViewController.h"
#import "MeAuthenticationTableViewCell.h"
#import "MeAutStatusViewController.h"
#import "taskUserManagerService.h"

@interface MeAuthenticationViewController ()<UITableViewDelegate,UITableViewDataSource,UITextFieldDelegate>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,strong)NSArray        *cellArray;
//认证信息
@property (nonatomic,strong)NSString       *identitycard;
@property (nonatomic,strong)NSString       *bankcard;
@property (nonatomic,strong)NSString       *alipay;
@property (nonatomic,strong)NSString       *userName;
@property (nonatomic,strong)NSString       *bankname;
@property (nonatomic,strong)NSString       *bankbranch;
@end

@implementation MeAuthenticationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"实名认证";
    self.cellArray = @[@[@"手机号码",@"姓名",@"身份证号",@"开户银行",@"银行卡号",@"开户支行",@"支付宝账号"],@[@" ",@"请输入真实姓名",@"请输入身份证号",@"请输入开户银行名称",@"请输入真实银行卡号",@"请输入支行名称",@"请输入与注册手机号一致的支付宝账号"]];
    [self.view addSubview:self.tableView];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapSliderAction)]];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self setBlackStatus:YES];
}

#pragma mark - init
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, distanceHeight, KScreenWidth, KScreenHeight-distanceHeight) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}
#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cellArray[0] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 100;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 100)];
    UIButton *nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    nextButton.frame = CGRectMake(60, 25, KScreenWidth - 120, 40);
    nextButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [nextButton setBackgroundImage:[UIImage imageNamed:@"icon_button"] forState:UIControlStateNormal];
    [nextButton setTitle:@"下一步" forState:UIControlStateNormal];
    [nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [nextButton addTarget:self action:@selector(autAction) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:nextButton];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 55;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *headView =[[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 55)];
    UILabel *lbtitle = [[UILabel alloc]initWithFrame:CGRectMake(15, 10, KScreenWidth - 30, 35)];
    lbtitle.numberOfLines = 0;
    lbtitle.text =@"注：身份认证信息务必真实，一旦有误将被封号";
    lbtitle.textAlignment = NSTextAlignmentLeft;
    lbtitle.textColor = COLOR_main;
    lbtitle.font = [UIFont systemFontOfSize:14];
    [headView addSubview:lbtitle];
    return headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 55;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MeAuthenticationTableViewCell *cell = [MeAuthenticationTableViewCell cellWithTableView:tableView];
    cell.textField.tag = indexPath.row;
    cell.textField.delegate = self;
    cell.textField.userInteractionEnabled = YES;
    cell.textField.keyboardType = UIKeyboardTypeDefault;
    cell.lbtitle.text =self.cellArray[0][indexPath.row];
    cell.textField.placeholder = self.cellArray[1][indexPath.row];
    switch (indexPath.row) {
        case 0:
        {
            if ([UserCenter shareCenter].user.phone.length > 0) {
                NSMutableString *string1 = [UserCenter shareCenter].user.phone.mutableCopy;
                [string1 replaceCharactersInRange:NSMakeRange(3, 6) withString:@"****"];
                cell.textField.text = string1;
                cell.textField.userInteractionEnabled = NO;
            }
        }
            break;
        case 1:
        {
            cell.textField.text =self.userName;
        }
            break;
        case 2:
        {
            cell.textField.text =self.identitycard;
            cell.textField.keyboardType =  UIKeyboardTypePhonePad;
        }
            break;
        case 3:
        {
            cell.textField.text =self.bankname;
        }
            break;
        case 4:
        {
            cell.textField.text =self.bankcard;
            cell.textField.keyboardType =  UIKeyboardTypePhonePad;
        }
            break;
        case 5:
        {
            cell.textField.text =self.bankbranch;
        }
            break;
        case 6:
        {
            cell.textField.text =self.alipay;
        }
            break;
        default:
            break;
    }
    return cell;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 1:
        {
            self.userName = textField.text;
        }
            break;
        case 2:
        {
            self.identitycard =textField.text;
        }
            break;
        case 3:
        {
            self.bankname = textField.text;
        }
            break;
        case 4:
        {
            self.bankcard = textField.text;
        }
            break;
        case 5:
        {
            self.bankbranch = textField.text;
        }
            break;
        case 6:
        {
            self.alipay =textField.text;
        }
            break;
        default:
            break;
    }
}

-(void)autAction
{
    [self.view endEditing:YES];
    if (![Check checkLenght:self.userName]) {
        [DKProgressHUD showErrorWithStatus:@"请输入真实姓名"];
        return;
    }
    if (![Check validateIdentityCard:self.identitycard]) {
        [DKProgressHUD showErrorWithStatus:@"请输入身份证号"];
        return;
    }
    if (![Check checkLenght:self.bankname]) {
        [DKProgressHUD showErrorWithStatus:@"请输入开户银行名称"];
        return;
    }
    if (!(self.bankcard.length > 14 && self.bankcard.length < 20)) {
        [DKProgressHUD showErrorWithStatus:@"请输入真实银行卡号"];
        return;
    }
    if (![Check checkLenght:self.bankbranch]) {
        [DKProgressHUD showErrorWithStatus:@"请输入支行名称"];
        return;
    }
    if (![Check checkLenght:self.alipay]) {
        [DKProgressHUD showErrorWithStatus:@"请输入与注册手机号一致的支付宝账号"];
        return;
    }
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setField:[NSString stringWithFormat:@"%@",[UserCenter shareCenter].user.phone] value:@"phone"];
    [dict setField:self.userName value:@"name"];
    [dict setField:self.identitycard value:@"identitycard"];
    [dict setField:self.bankcard value:@"bankcard"];
    [dict setField:self.alipay value:@"alipay"];
    [dict setField:self.bankbranch value:@"bankbranch"];
    [dict setField:self.bankname value:@"bankname"];
    [taskUserManagerService getgeneralAuth:dict success:^(BaseModel *response, BOOL success) {
        if (success) {
            [self.navigationController pushViewController:[MeAutStatusViewController new] animated:YES];
            [[UserCenter shareCenter] refreshProfiles:^(dataGetUserInfo *profile) {
                
            }];
        }else{
            [DKProgressHUD showErrorWithStatus:response.message];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(void)tapSliderAction
{
    [self.view endEditing:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
